#include "Platform.h"
#include "BMPLoader.h"
#include <GL/glut.h>
#include <iostream>
using namespace std;

GLuint Platform::texID = -1;

GLfloat Platform::normals[] = {1,1,1,
							   1,-1,1,
							   -1,-1,1
							   -1,1,1,

							   -1,1,-1,
							   1,1,-1,

							   1,-1,-1,

							   -1,-1,-1};

Platform::Platform(float w, float h): zDim(200)
{
	position.x = 0;
	position.y = 0;

	if (texID == -1)
	{
		
		BMPClass bmp;
		BMPLoad("metal.BMP",bmp);
		glGenTextures(1, &texID);
		glBindTexture(GL_TEXTURE_2D, texID);

		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); 
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); 
		
		glTexImage2D(GL_TEXTURE_2D,0,3,bmp.width,bmp.height,0,GL_RGB,GL_UNSIGNED_BYTE,bmp.bytes);
		
		
	}
	
		
	glBindTexture(GL_TEXTURE_2D, texID);
	int tWidth, tHeight;
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &tWidth);
	glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_HEIGHT, &tHeight);

	float platW = w * 2.f;
	float platH = h * 2.f;

	texS = platW / tWidth;
	texT = platH / tHeight;
	


	cw = w;
	ch = h;
}

void Platform::draw()
{
	glMatrixMode(GL_MODELVIEW);
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, texID);
	glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); 
	glPushMatrix();
	glTranslatef(position.x,position.y,0);
	glScalef(sx,sy,sz);
	glBegin(GL_QUADS);
		// Front Face
		glNormal3f(-1,-1,1); glTexCoord2f(0.0f, 0.0f); glVertex3f(-cw, -ch,  zDim);	// Bottom Left Of The Texture and Quad
		glNormal3f(1,-1,1); glTexCoord2f(texS, 0.0f); glVertex3f( cw, -ch,  zDim);	// Bottom Right Of The Texture and Quad
		glNormal3f(1,1,1); glTexCoord2f(texS, texT); glVertex3f( cw,  ch,  zDim);	// Top Right Of The Texture and Quad
		glNormal3f(-1,1,1); glTexCoord2f(0.0f, texT); glVertex3f(-cw,  ch,  zDim);	// Top Left Of The Texture and Quad
		// Back Face
		glNormal3f(-1,-1,-1); glTexCoord2f(texS, 0.0f); glVertex3f(-cw, -ch, -zDim);	// Bottom Right Of The Texture and Quad
		glNormal3f(-1,1,-1); glTexCoord2f(texS, texT); glVertex3f(-cw,  ch, -zDim);	// Top Right Of The Texture and Quad
		glNormal3f(1,1,-1); glTexCoord2f(0.0f, texT); glVertex3f( cw,  ch, -zDim);	// Top Left Of The Texture and Quad
		glNormal3f(1,-1,-1); glTexCoord2f(0.0f, 0.0f); glVertex3f( cw, -ch, -zDim);	// Bottom Left Of The Texture and Quad
		// Top Face
		glNormal3f(-1,1,-1); glTexCoord2f(0.0f, texT); glVertex3f(-cw,  ch, -zDim);	// Top Left Of The Texture and Quad
		glNormal3f(-1,1,1); glTexCoord2f(0.0f, 0.0f); glVertex3f(-cw,  ch,  zDim);	// Bottom Left Of The Texture and Quad
		glNormal3f(1,1,1); glTexCoord2f(texS, 0.0f); glVertex3f( cw,  ch,  zDim);	// Bottom Right Of The Texture and Quad
		glNormal3f(1,1,-1); glTexCoord2f(texS, texT); glVertex3f( cw,  ch, -zDim);	// Top Right Of The Texture and Quad
		// Bottom Face
		glNormal3f(-1,-1,-1); glTexCoord2f(texS, texT); glVertex3f(-cw, -ch, -zDim);	// Top Right Of The Texture and Quad
		glNormal3f(1,-1,-1); glTexCoord2f(0.0f, texT); glVertex3f( cw, -ch, -zDim);	// Top Left Of The Texture and Quad
		glNormal3f(1,-1,-1); glTexCoord2f(0.0f, 0.0f); glVertex3f( cw, -ch,  zDim);	// Bottom Left Of The Texture and Quad
		glNormal3f(-1,-1,1); glTexCoord2f(texS, 0.0f); glVertex3f(-cw, -ch,  zDim);	// Bottom Right Of The Texture and Quad
		// Right face
		glNormal3f(1,-1,-1); glTexCoord2f(texS, 0.0f); glVertex3f( cw, -ch, -zDim);	// Bottom Right Of The Texture and Quad
		glNormal3f(1,1,-1); glTexCoord2f(texS, texT); glVertex3f( cw,  ch, -zDim);	// Top Right Of The Texture and Quad
		glNormal3f(1,1,1); glTexCoord2f(0.0f, texT); glVertex3f( cw,  ch,  zDim);	// Top Left Of The Texture and Quad
		glNormal3f(1,-1,1); glTexCoord2f(0.0f, 0.0f); glVertex3f( cw, -ch,  zDim);	// Bottom Left Of The Texture and Quad
		// Left Face
		glNormal3f(-1,-1,-1); glTexCoord2f(0.0f, 0.0f); glVertex3f(-cw, -ch, -zDim);	// Bottom Left Of The Texture and Quad
		glNormal3f(-1,-1,1); glTexCoord2f(texS, 0.0f); glVertex3f(-cw, -ch,  zDim);	// Bottom Right Of The Texture and Quad
		glNormal3f(-1,1,1); glTexCoord2f(texS, texT); glVertex3f(-cw,  ch,  zDim);	// Top Right Of The Texture and Quad
		glNormal3f(-1,1,-1); glTexCoord2f(0.0f, texT); glVertex3f(-cw,  ch, -zDim);	// Top Left Of The Texture and Quad
	glEnd();
	glPopMatrix();
	//glDrawElements(GL_QUADS,24,GL_UNSIGNED_BYTE,indeces);
	glDisable(GL_TEXTURE_2D);
}